package wc;

import java.io.*;
import java.util.ArrayList;
import java.util.TreeMap;

/**
 * Author: wuhen
 * Date: 2018/10/16
 * Time: 9:19
 */
public class BasicInfoWithSL {
    public static Wc getBasicInfoWithSL(String fileName,String stopList)
    {
        Wc wc=new Wc();

        ArrayList<String> wordsIgnored = new ArrayList<String>();
        wordsIgnored.add(stopList);
        StopList.getStopList(wordsIgnored);

        //定义一个map集合保存stoplist中的单词
        TreeMap<String,Integer> tm = new TreeMap<String,Integer>();

        for(int i=0;i<wordsIgnored.size();i++)
        {
            String word=wordsIgnored.get(i);
            tm.put(word,1); /////////
        }
        try
        { // 防止文件建立或读取失败，用catch捕捉错误并打印，也可以throw
            File filename = new File(fileName); // 要读取该路径的文件
            InputStreamReader reader = new InputStreamReader(
                    new FileInputStream(filename)); // 建立一个输入流对象reader
            BufferedReader br = new BufferedReader(reader); // 建立一个对象，它把文件内容转成计算机能读懂的语言
            String line ;
            line = br.readLine();
            while (line != null)
            {
                //——————————————————————统计行数和字符数__________
                wc.chars+=line.length();
                wc.lines++;

                //________________________统计单词_____________________________

                line.toLowerCase();
                String reg1 = "\\s+|,+";   //用空格、tab或逗号来分割单词
                //String regEmp = "\\s+";
                //将读取的文本进行分割
                String str[] = line.split(reg1);

                for(String s: str)
                {
                    if(!s.equals(""))
                    {

                        //判断tm即stopList中中是否已经存在该单词，如果不存在则单词数加一；若存在则不变
                        if(!tm.containsKey(s))   //tm中是否包含该单词
                        {
                            //System.out.println(s);
                            wc.words++;
                        }
                    }

                }

                //System.out.println(tm);
                //-----------------------------------------------------------------
                line = br.readLine(); // 读取下一行数据
            }
            wc.chars+=wc.lines-1;    //由于换行符也算做字符，故如此
            br.close();
        }

        catch (IOException e)
        {
            e.printStackTrace();
        }
        return wc;
    }

}
